package singsong;

import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JPanel;
import javax.swing.JTextField;

public class TrackController implements ActionListener {

	private Track track;
	private JPanel panel;
	private JTextField nameField;
	private JCheckBox audibleCheckBox;
	private JComboBox instrumentComboBox;
	
	public TrackController(Track track) {
		this.track = track;
		
		panel = new JPanel();
		panel.setLayout(new FlowLayout());
		
		panel.add(makeButton("X", this, "delete"));
		
		nameField = new JTextField(track.getName(), 15);
		nameField.setActionCommand("name");
		nameField.addActionListener(this);
		panel.add(nameField);
		
		audibleCheckBox = new JCheckBox("audible", track.isAudible());
		audibleCheckBox.setActionCommand("audible");
		audibleCheckBox.addActionListener(this);
		panel.add(audibleCheckBox);
		
		String[] instrumentStrings = { "none", "guitar" };
		instrumentComboBox = new JComboBox(instrumentStrings);
		instrumentComboBox.setActionCommand("instrument");
		instrumentComboBox.addActionListener(this);
		panel.add(instrumentComboBox);
	}
	
	private JButton makeButton(String label, ActionListener listener, String command) {
		JButton b = new JButton(label);
		b.addActionListener(listener);
		b.setActionCommand(command);
		return b;
	}

	public void actionPerformed(ActionEvent event) {
		if (event.getActionCommand().equals("delete")) {
			deleteListener.delete(this);
		} else if (event.getActionCommand().equals("name")) {
			track.setName(nameField.getText());
		} else if (event.getActionCommand().equals("audible")) {
			track.setAudible(audibleCheckBox.isSelected());
		} else if (event.getActionCommand().equals("instrument")) {
			track.recordClip();
		}
	}
	
	public JPanel getPanel() {
		return panel;
	}
	
	public Track getTrack() {
		return track;
	}

	public interface DeleteListener {
		public void delete(TrackController trackController);
	}
	
	DeleteListener deleteListener = null;

	public void addDeleteListener(DeleteListener deleteListener) {
		this.deleteListener = deleteListener;
	}
}
